package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.SaleListAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.SaleListBean;
import dhm.com.dhmshop.utils.SpUtils;

public class SalelistActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.recy_data)
    XRecyclerView recyData;
    private PressenterImpl pressenter;
    int page = 1;
    private List<SaleListBean.DataBean> saleList;
    private SaleListAdapter saleListAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_salelist;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        saleListAdapter = new SaleListAdapter(this);
        recyData.setAdapter(saleListAdapter);

        recyData.setLoadingMoreEnabled(true);
        recyData.setPullRefreshEnabled(true);
        recyData.setLayoutManager(manager);
        recyData.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });
        page = 1;
        getdata();
    }

    private void getdata() {
        String uid = SpUtils.getString(this, "uid");
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("page", page + "");
        map.put("uid",uid);
        pressenter.sendMessage(this, Constant.ServiceList, map, SaleListBean.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof SaleListBean) {
            SaleListBean saleListBean = (SaleListBean) data;

            if (saleListBean.getCode()==1){
                if (page == 1) {
                    saleList = saleListBean.getData();

                } else {
                    saleList.addAll(saleListBean.getData());
                }
                saleListAdapter.setShopList(saleList);
                page++;
            }

            recyData.loadMoreComplete();
            recyData.refreshComplete();

        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
